﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.Temp
{
	internal class Search
	{
		public static Reference PickHvacElement(string message, UIDocument uiDoc)
		{
			Reference result;
			try
			{
				Reference reference;
				for (;;)
				{
					reference = uiDoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, message);
					if (reference != null && reference.ElementId != ElementId.InvalidElementId)
					{
						Element elementById = uiDoc.Document.GetElementById(reference.ElementId);
						XYZ globalPoint = reference.GlobalPoint;
						if (elementById == null || RevitUtils.IsPipeSystemObject(elementById))
						{
							break;
						}
						message = "所选实体不是水系统实体，请重新选择";
					}
				}
				result = reference;
			}
			catch (Exception ex)
			{
				string message2 = ex.Message;
				result = null;
			}
			return result;
		}

		public bool BeMinimizeOperator(UIDocument uiDoc, ICollection<ElementId> ElementIds)
		{
			HYTwinkle hytwinkle = new HYTwinkle();
			hytwinkle.View = uiDoc.ActiveView;
			hytwinkle.Ids = ElementIds;
			hytwinkle.Start();
			if (this.BeMinimize(uiDoc))
			{
				hytwinkle.Dispose();
			}
			return true;
		}

		private bool BeMinimize(UIDocument uiDoc)
		{
			bool result;
			try
			{
				uiDoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)0, "查看状态，不能进行任何操作！");
				result = true;
			}
			catch (Exception)
			{
				result = true;
			}
			return result;
		}

		public static Connector GetNearestConnector(Element elem, XYZ ptSel)
		{
			ElementNode elementNode = new ElementNode(elem);
			double num = ptSel.DistanceTo(elementNode.ConnectorIn.Origin);
			double num2 = ptSel.DistanceTo(elementNode.ConnectorOut.Origin);
			if (num <= num2)
			{
				return elementNode.ConnectorIn;
			}
			return elementNode.ConnectorOut;
		}

		private ElementNode PickFirst(UIDocument uiDoc)
		{
			Reference reference = Search.PickHvacElement("请选择要计算分支的第一段管远端：", uiDoc);
			if (reference == null)
			{
				return null;
			}
			Element elementById = uiDoc.Document.GetElementById(reference.ElementId);
			if (elementById == null)
			{
				return null;
			}
			XYZ globalPoint = reference.GlobalPoint;
			Connector nearestConnector = Search.GetNearestConnector(elementById, globalPoint);
			return new ElementNode(elementById, nearestConnector);
		}

		public bool Run(UIDocument uiDoc)
		{
			if (uiDoc == null)
			{
				return false;
			}
			this.m_UIDocument = uiDoc;
			bool result;
			try
			{
				this.searchSystem = new SearchSystem(uiDoc, SearchSystemFilter.ByName);
				if (!this.searchSystem.Run("请选择要计算分支的第一段管远端: "))
				{
					result = false;
				}
				else
				{
					this.m_ndRoot = this.searchSystem.ConvertToSearchBranchSystem();
					this.m_SystemName = RevitUtils.GetSystemName(this.m_ndRoot.Element);
					result = (this.m_ndRoot != null);
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show("搜索失败，管道连接可能不正确!" + ex.Message);
				result = false;
			}
			return result;
		}

		public bool DumpIntoPipeSystem(out HYPipeSystem system)
		{
			if (this.m_ndRoot == null)
			{
				throw new Exception("Has no root object，cannot DumpIntoPipeSystem");
			}
			bool result;
			try
			{
				system = new HYPipeSystem();
				system.Name = this.m_SystemName;
				HYMEPObject hymepobject = this.m_ndRoot.CreateObject(ref system);
				if (hymepobject == null)
				{
					YJKMessageBox.Show("水系统水力计算仅适用于风机盘管组成的水系统！");
					system = null;
					result = false;
				}
				else
				{
					system.SetRootObject(hymepobject);
					system.BuildBranches();
					system.BuildIndex();
					result = true;
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				YJKMessageBox.Show("分支搜索失败，请检查系统连接情况，保证管道连接完整！\n" + message);
				system = null;
				result = false;
			}
			return result;
		}

		private UIDocument m_UIDocument;

		private SearchSystem searchSystem;

		private ElementNode m_ndRoot;

		public string m_SystemName;
	}
}
